package artificial

import (
	"bytes"
	"fmt"
	"net/http"
	"parking-lot-go-admin/config"
	"parking-lot-go-admin/controller"
	"parking-lot-go-admin/model"
	"parking-lot-go-admin/model/cars"
	"parking-lot-go-admin/model/order"
	"parking-lot-go-admin/model/park"

	"github.com/gin-gonic/gin"
)

// 进场记录列表
func OutList(c *gin.Context) {
	requestDataMap := make(map[string]interface{})
	c.ShouldBindJSON(&requestDataMap)

	where, page, pageSize := controller.WhereStructure(requestDataMap)

	//查询信息
	var carsOutStruct cars.CarsOutStruct
	var CarsOutData []cars.CarsOutStruct
	model.GetModelData(
		model.Db,
		carsOutStruct.TableName(),
		&CarsOutData,
		model.QueryOptions{
			Page:       page,
			PageSize:   pageSize,
			Conditions: where,
			OrderBy:    "id desc",
			Debug:      true,
		},
	)

	//数据格式化
	returnData := make([]map[string]interface{}, 0)
	for _, v := range CarsOutData {
		returnData = append(returnData, map[string]interface{}{
			"id":           v.Id,
			"park_code":    v.ParkCode,
			"park_name":    v.ParkName,
			"car_number":   v.CarNumber,
			"car_no_color": v.CarNoColor,
			"out_time":     v.OutTime.Format("2006-01-02 15:04:05"),
			"order_type":   v.OrderType,
			"order_no":     v.OrderNo,
			"much_id":      v.MuchId,
		})
	}

	//总数查询
	count, _ := model.CountModelData(
		model.Db,
		carsOutStruct.TableName(),
		model.QueryOptions{
			Conditions: where,
		},
	)

	controller.Success(c,
		map[string]interface{}{
			"list":        returnData,
			"total":       count,
			"pageSize":    pageSize,
			"currentPage": page,
		})
}

// 添加出场记录
func OutAdd(c *gin.Context) {
	requestDataStruct := struct {
		CarNumber  string `json:"car_number"`
		CarNoColor int    `json:"car_no_color"`
		ParkCode   int    `json:"park_code"`
		OutTime    string `json:"out_time"`
		MuchId     string `json:"much_id"`
	}{}

	c.ShouldBindJSON(&requestDataStruct)

	//获取订单信息
	var orderStruct order.OrderStruct
	model.GetModelDataFirst(
		model.Db,
		orderStruct.TableName(),
		&orderStruct,
		model.QueryOptions{
			Conditions: map[string]interface{}{
				"status":           1,
				"car_number":       requestDataStruct.CarNumber,
				"car_no_color":     requestDataStruct.CarNoColor,
				"static_park_code": requestDataStruct.ParkCode,
			},
			Debug: true,
		})

	if orderStruct.Id == 0 {
		controller.Fail(c, "订单查询失败")
		return
	}

	//没查到入场时间 按创建时间返回
	if orderStruct.InTime.IsZero() {
		orderStruct.InTime = orderStruct.CreatedAt
	}

	var parkingLotStruct park.ParkingLotStruct
	model.GetModelDataFirst(
		model.Db,
		parkingLotStruct.TableName(),
		&parkingLotStruct,
		model.QueryOptions{
			Conditions: map[string]interface{}{
				"park_code": requestDataStruct.ParkCode,
			},
		})

	// 车辆进场
	go func() {
		//拼接url
		systemSection := config.Cfg.Section("SYSTEM")
		url := systemSection.Key("INTERFACE_URL").String() + "third/out"
		jsonData := []byte(
			fmt.Sprintf(`{"carId": "%s", "carnoColor": %d, "inTime": "%s", "muchId": "%s","outTime":"%s","parkCode":"%d","parkName":"%s","is_admin":1}`,
				requestDataStruct.CarNumber, requestDataStruct.CarNoColor, orderStruct.InTime.Format("2006-01-02 15:04:05"), requestDataStruct.MuchId, requestDataStruct.OutTime, requestDataStruct.ParkCode, parkingLotStruct.Name))

		//发送请求
		resp, _ := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
		defer resp.Body.Close()
	}()

	controller.Success(c, "")
}
